package heima3_2.cn.osc.ui.main_fragment;


import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;


import com.google.zxing.activity.CaptureActivity;

import net.oschina.app.R;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

import heima3_2.cn.osc.ui.activity.ExerciseActivity;
import heima3_2.cn.osc.ui.activity.FriendsActivity;
import heima3_2.cn.osc.ui.activity.SearchPersonActivity;
import heima3_2.cn.osc.ui.activity.ShakeActivity;

import static android.app.Activity.RESULT_OK;


/**
 * Created by emily on 2017/1/14.
 */

public class findFragment extends Fragment {
    public static final int REQUEST_CODE = 0;
    @Bind(R.id.rl_friends_group)
    RelativeLayout rlFriendsGroup;
    @Bind(R.id.rl_serch_person)
    RelativeLayout rlSerchPerson;
    @Bind(R.id.rl_activity)
    RelativeLayout rlActivity;
    @Bind(R.id.rl_scan)
    RelativeLayout rlScan;
    @Bind(R.id.rl_shake)
    RelativeLayout rlShake;
    @Bind(R.id.activity_main)
    LinearLayout activityMain;
    private Intent intent;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View itemView = inflater.inflate(R.layout.fragment_find, container, false);
        ButterKnife.bind(this, itemView);
        return itemView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.rl_friends_group, R.id.rl_serch_person, R.id.rl_activity, R.id.rl_scan, R.id.rl_shake, R.id.activity_main})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_friends_group:
                rlFriendsGroup.setSelected(true);
                Toast.makeText(getActivity(), "朋友圈", Toast.LENGTH_SHORT).show();
                intent = new Intent(getActivity(), FriendsActivity.class);
                startActivity(intent);
                break;
            case R.id.rl_serch_person:
                rlSerchPerson.setSelected(true);
                intent = new Intent(getActivity(), SearchPersonActivity.class);
                startActivity(intent);
                break;
            case R.id.rl_activity:
                rlSerchPerson.setSelected(true);
                intent = new Intent(getActivity(), ExerciseActivity.class);
                startActivity(intent);
                break;
            case R.id.rl_scan:
                Scan();

                break;
            case R.id.rl_shake:
                rlSerchPerson.setSelected(true);
                intent=new Intent(getActivity(), ShakeActivity.class);
                startActivity(intent);
                break;

        }

    }

    private void Scan() {
        Intent intent = new Intent(getActivity(), CaptureActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivityForResult(intent, REQUEST_CODE);
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK) { //RESULT_OK = -1
            Bundle bundle = data.getExtras();
            String scanResult = bundle.getString("result");
            Toast.makeText(getActivity(), scanResult, Toast.LENGTH_LONG).show();
            //意图
            Intent intent = new Intent();
            //意图的行为，隐式意图
            intent.setAction(Intent.ACTION_VIEW);
            //意图的数据
            intent.setData(Uri.parse(scanResult));
            //启动
            startActivity(intent);

        }
    }
}









